package com.xyyl.patient.service.user;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xyyl.patient.core.constant.AppEnum;
import com.xyyl.patient.core.util.DateUtils;
import com.xyyl.patient.entity.user.AccountRecord;
import com.xyyl.patient.entity.user.ActivityUser;
import com.xyyl.patient.entity.user.User;
import com.xyyl.patient.mapper.user.AccountRecordMapper;
import com.xyyl.patient.mapper.user.ActivityUserMapper;
import com.xyyl.patient.mapper.user.UserMapper;

/**
 * @projectName:xyyl-oms-server
 * @ClassName: ActivityUser
 * @Description:ActivityUser
 * @author chengjun
 * @date 2017-11-22 15:44:00
 */
@Service("activityUserService")
public class ActivityUserServiceImpl implements ActivityUserService {

	@Autowired
	private ActivityUserMapper activityUserMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private AccountRecordMapper accountRecordMapper;

	 @Override
	 public ActivityUser getActivityUser(Integer id) {
		 ActivityUser activityUser =activityUserMapper.selectOne(id);
		 return activityUser;

	 }
	 @Override
	 public Integer getNewPatientRedPacket(Integer id){
	 	//查用户是否存在, 和用户是否新用户
		 User user = userMapper.queryUserByOption(id, null);
		 ActivityUser activityUser1 =activityUserMapper.selectOne(id);
		 if(user !=null &&activityUser1 ==null){
			 ActivityUser activityUser = new ActivityUser();
			 activityUser.setPatientId(id);
			 activityUser.setActId(1);
			 Long l =System.currentTimeMillis();
			 activityUser.setAddTime(Integer.valueOf(l.toString().substring(0,10)));
			 activityUser.setPrice(new BigDecimal(50));
			 //插入领取红包记录表
 			 activityUserMapper.insert(activityUser);

			 //插入流水记录表
			 AccountRecord accountRecord = new AccountRecord();
			 accountRecord.setCreatetime(DateUtils.currentDatetime());
			 accountRecord.setAmount(new BigDecimal(50));
			 accountRecord.setChangetype(AppEnum.common.Order_changetype_in.getValue());
			 accountRecord.setIncometype(1);
			 accountRecord.setPatientid(user.getId());
			 accountRecord.setTotalamountAfter(new BigDecimal(user.getAccountbalance().intValue()+50));
			 accountRecord.setTotalamountBefore(user.getAccountbalance());
			 accountRecordMapper.insert(accountRecord);

			 user.setAccountbalance(new BigDecimal(user.getAccountbalance().intValue()+50));
			 //更新用户表余额
			 userMapper.update(user);
			 return 1;
		 }else {
		 	return 0;
		 }


	 }

}
